home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 029a / mob_trim.zip / MOB-TRIM.C next >
Text File  |  1991-04-16  |  5KB  |  231 lines

  1. /*  This source code is released in the Public Domain.
  2.     Under no circumstances is a fee to be charged for this code,
  3.     or any program which may be based upon this code.
  4.     You may, on the other hand, do whatever you wish with this file,
  5.     so long as it remains free.
  6.  
  7.     This code compiles under MS QC 2.51 and -may- compile under TurboC.
  8.     (I do not have TC to test with)
  9.  
  10.      Command-line that I used to compile program:
  11.         qcl /AT /Ox mob-trim.c /link /packc
  12.  
  13.     Written by Nathan Hartwell (Fidonet 1:362/116)
  14.     Corrections done with help from Shawn Stoddard (Fidonet 1:362/101)
  15.  
  16.     Copyright (c) 1991 Portal Systems Inc.
  17.     All Rights Reserved
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <conio.h>
  23. #include <dos.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26.  
  27. int how_old(char *m, int d);
  28. int get_ln(char *fn);
  29. int get_la(void);
  30.  
  31. static unsigned days[2][14] = {
  32.     {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
  33.     {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
  34. };
  35.  
  36. static char lognames[240], *lnp;
  37. static char logages[120], *lap;
  38.  
  39. main(argc, argv)
  40. int argc;
  41. char **argv;
  42. {
  43.     FILE *in, *out, *cfg;
  44.     char s[256], lfile[80], *sp, *sp1;
  45.     char strp[256][80], logmonth[4];
  46.     int curptr = 0, i, clr, sl, sl0, sl1, age, age1, day;
  47.     int kilt, old_kilt, kpblnk, percent, old_percent;
  48.     long sz1, sz2;
  49.  
  50.     cputs("MOB-Trim - Log Trimming Utility v2.00ß\r\n");
  51.     cputs("Copyright (C) 1991 PSI  All Rights Reserved\r\n");
  52.     cputs("Written by Nathan Hartwell\r\n\n");
  53.  
  54.     if (argc > 1) cfg = fopen(argv[1], "rt");
  55.     else cfg = fopen("mob-trim.cfg", "rt");
  56.     if (cfg == NULL) {
  57.         cprintf("\aUnable to open config file: %s\r\n", (argc == 1) ? "mob-trim.cfg" : argv[1]);
  58.         exit(1);
  59.     }
  60.  
  61.     fgets(lognames, 240, cfg);
  62.     lnp = lognames;
  63.  
  64.     fgets(logages, 120, cfg);
  65.     lap = logages;
  66.  
  67.     while (1) {
  68.         fgets(s, 80, cfg);
  69.         if (feof(cfg)) break;
  70.         if (strlen(s) < 3) continue;
  71.         s[strlen(s)-1] = 0;
  72.         strcpy(strp[curptr++], s);
  73.     }
  74.     fclose(cfg);
  75.  
  76.     if (curptr == 0) {
  77.         cputs("\aNothing to do!  Exiting....\r\n\n");
  78.         exit(2);
  79.     }
  80.  
  81.     age = 0;
  82.  
  83.     while (get_ln(lfile)) {
  84.  
  85.         if ((age1 = get_la()) != 0) age = age1;
  86.  
  87.         unlink("temp.log");
  88.         kilt = old_kilt = 0;
  89.         kpblnk = old_percent = 0;
  90.         clr = -1;
  91.  
  92.         in = fopen(lfile, "rt");
  93.         if (in == NULL) {
  94.             cprintf("\aUnable to open log file: %s\r\n\n", lfile);
  95.             exit(1);
  96.         }
  97.         fseek(in, 0L, SEEK_END);
  98.         sz1 = ftell(in);
  99.         fseek(in, 0L, SEEK_SET);
  100.         out = fopen("temp.log", "wt");
  101.         if (out == NULL) {
  102.             cputs("\aError opening temporary file.\r\n\n");
  103.             exit(1);
  104.         }
  105.  
  106.         cprintf("Trimming log: %s\r\n", lfile);
  107.         if (age) cprintf("Trimming out entries %d day%s and older.\r\n", age, age == 1 ? "" : "s");
  108.  
  109.         while (1) {
  110. top:
  111.             fgets(s, 255, in);
  112.             if (feof(in)) break;
  113.             sz2 = ftell(in);
  114.             if (strlen(s) < 3) {
  115.                 if (kpblnk) fputs(s, out);
  116.                 goto top;
  117.             }
  118.             sp = s + 20;
  119.             while (*sp != ' ') sp++;
  120.             while (*sp == ' ') sp++;
  121.             sl = strlen(sp) - 1;
  122.             sp1 = s + 2;
  123.             if (!isdigit(*sp1)) sp1++;
  124.             day = atoi(sp1);
  125.             strncpy(logmonth, s+5, 3);
  126.             logmonth[3] = 0;
  127.             age1 = how_old(logmonth, day);
  128.             if (age && age1 >= age) {
  129.                 clr = 0;
  130.                 goto _jmp;
  131.             }
  132.             if (!kpblnk && age1 <= age) kpblnk = 1;
  133.             for (i = 0; i < curptr; i++) {
  134.                 if (s[0] == strp[i][0]) {
  135.                     sp1 = strp[i] + 2;
  136.                     sl1 = strlen(sp1);
  137.                     sl0 = (sl < sl1) ? sl : sl1;
  138.                     if (strncmp(sp, sp1, sl0) == 0) clr = 0;
  139.                 }
  140.                 if (clr != -1) break;
  141.             }
  142. _jmp:
  143.             if (clr == -1) fputs(s, out);
  144.             else kilt++;
  145.             percent = ((sz2 * 100) / sz1);
  146.             if (percent != old_percent || kilt != old_kilt)
  147.                 cprintf("Completed: %3d%%   Lines Removed: %d\r", percent, kilt);
  148.             old_percent = percent;
  149.             old_kilt = kilt;
  150.             clr = -1;
  151.         }
  152.  
  153.         cputs("\r\n\n");
  154.         fclose(in);
  155.         fclose(out);
  156.         unlink(lfile);
  157.         rename("temp.log", lfile);
  158.     }
  159. }
  160.  
  161. int
  162. how_old(m, d)
  163. char *m;
  164. int d;
  165. {
  166.     int i, ii, leap;
  167. #ifdef __TURBOC__
  168.     struct date today;
  169.  
  170.     getdate(&today);
  171.     leap = ((today.da_year % 4) == 0);
  172.     i = (days[leap][today.da_mon]) + today.da_day;
  173. #else
  174.     struct dosdate_t today;
  175.  
  176.     _dos_getdate(&today);
  177.     leap = ((today.year % 4) == 0);
  178.     i = (days[leap][today.month]) + today.day;
  179. #endif
  180.  
  181.     if (strcmp(m, "Jan") == 0) ii = 0;
  182.     else if (strcmp(m, "Feb") == 0) ii = days[leap][2];
  183.     else if (strcmp(m, "Mar") == 0) ii = days[leap][3];
  184.     else if (strcmp(m, "Apr") == 0) ii = days[leap][4];
  185.     else if (strcmp(m, "May") == 0) ii = days[leap][5];
  186.     else if (strcmp(m, "Jun") == 0) ii = days[leap][6];
  187.     else if (strcmp(m, "Jul") == 0) ii = days[leap][7];
  188.     else if (strcmp(m, "Aug") == 0) ii = days[leap][8];
  189.     else if (strcmp(m, "Sep") == 0) ii = days[leap][9];
  190.     else if (strcmp(m, "Oct") == 0) ii = days[leap][10];
  191.     else if (strcmp(m, "Nov") == 0) ii = days[leap][11];
  192.     else if (strcmp(m, "Dec") == 0) ii = days[leap][12];
  193.  
  194.     ii += d;
  195.  
  196.     return (i - ii);
  197. }
  198.  
  199. int
  200. get_ln(fn)
  201. char *fn;
  202. {
  203.     if (*lnp == '\0' || *lnp == '\n' || *lnp == '\r') return (0);
  204.  
  205.     while (*lnp == ' ') lnp++;
  206.  
  207.     while (*lnp && *lnp != ' ' && *lnp != '\n' && *lnp != '\r')
  208.         *fn++ = *lnp++;
  209.  
  210.     *fn = '\0';
  211.     return (1);
  212. }
  213.  
  214. int
  215. get_la()
  216. {
  217.     char v[8], *vp;
  218.  
  219.     vp = v;
  220.  
  221.     if (*lap == '\0' || *lap == '\n' || *lap == '\r') return (0);
  222.  
  223.     while (*lap == ' ') lap++;
  224.  
  225.     while (*lap && *lap != ' ' && *lap != '\n' && *lap != '\r')
  226.         *vp++ = *lap++;
  227.  
  228.     *vp = '\0';
  229.     return (atoi(v));
  230. }
  231.